﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2022-2022 LiLong                                              **
**  This file is part of BluetoothCpp.                                          **
**                                                                              **
**  BluetoothCpp is free software: you can redistribute it and/or modify        **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  BluetoothCpp is distributed in the hope that it will be useful,             **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with BluetoothCpp.  If not, see <https://www.gnu.org/licenses/>.      **
**********************************************************************************/
#include "Socket.h"
#include "Private/SocketWin.h"

namespace BTH
{

struct Socket::Impl
{
#ifdef WIN32
    SocketWin socket;
#else
#error Unsupported platform
#endif
    inline Impl() {}
    inline Impl(Handle handle) : socket(handle) {}
};

Socket::Socket() : m_impl(std::make_unique<Impl>()) {}

Socket::Socket(Socket&& other) { std::swap(m_impl, other.m_impl); }

Socket::Socket(Handle handle) : m_impl(std::make_unique<Impl>(handle)) {}

Socket::~Socket() {}

void Socket::connect(const Address& addr, const Uuid& uuid) { m_impl->socket.connect(addr, uuid); }

bool Socket::isConnected() const { return m_impl->socket.isConnected(); }

void Socket::send(const char* buffer, int size) { m_impl->socket.send(buffer, size); }

void Socket::close() { m_impl->socket.close(); }

std::string Socket::read(int maxSize) { return m_impl->socket.read(maxSize); }

Handle Socket::handle() const { return m_impl->socket.handle(); }

void Socket::setSendTimeout(const std::chrono::milliseconds& msec) { m_impl->socket.setSendTimeout(msec); }

void Socket::setRecvTimeout(const std::chrono::milliseconds& msec) { m_impl->socket.setRecvTimeout(msec); }

} // namespace BTH
